package com.orhon.smartcampus.modules.teacher.mutation;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.teacher.dao.OfficeArrangeUserDao;
import com.orhon.smartcampus.modules.teacher.entity.OfficeArrangeUser;
import com.orhon.smartcampus.modules.teacher.gqlInputModel.GQLArrangeTeacherOfficeInput;
import com.orhon.smartcampus.modules.teacher.inputMapper.ArrangeTeacherOfficeInputMapper;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class AddOfficeArrangeUserMutation implements DataFetcher {

    @Autowired
    OfficeArrangeUserDao officeArrangeUserDao;

    /**
     * 新增/修改分配办公室信息
     * editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {
        GQLArrangeTeacherOfficeInput input = ArrangeTeacherOfficeInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));

        OfficeArrangeUser officeArrangeUser = null;

        officeArrangeUser = new OfficeArrangeUser();
        officeArrangeUser.setCreatedAt(new Date());
        if(input.getRoom_id() != null){

            officeArrangeUser.setRoomId(input.getRoom_id());
        }
        if(input.getUser_id() != null){

            officeArrangeUser.setUserId(input.getUser_id());
        }
        try{
            officeArrangeUserDao.saveOrUpdate(officeArrangeUser);
        }
        catch (Exception e){
            return Result.error(500,""+e);
        }
        return Result.ok();
    }
}

